********************************************************************************
*** Merge RI data with cleaned data	
********************************************************************************
	
	use "$covidclean/smscovid_clean.dta", clear

	keep if consent==1 & age>=18
	keep if ~missing(treatment_arm)

	*Drop irrelevant variables
	drop *_c *_s *_oth submissiondate-endtime call_later call_status_others SURVEY_OTHERRELATED-phonecut_sec8
	drop IDENTIFIERS TREATMENT SURVEY_RELATED SAMPLE_RELATED DEMOGRAPHICS SYMPTOMS KNOWLEDGE_BEHAVIOR ACTION_BEHAVIOR PROMPTED_BEHAVIOR PROMPTED_BEHAVIOR_COMM LISTEXPERIMENT SMSRELATED RISKPERCEPTIONS MARLOWECROWE ROUTINECARE SMSDELIVERYSTATUS
	drop treatment_*
	
	***Merge randomisation inference data with the clean data
		merge 1:m scrambledid using "$rianalysis/ri_data_stratarand.dta", keepusing(treatment batch newround)
		
		***TREATMENTS
			
			assert !missing(treatment)
			
			***10 treatment arms + 1 control
			rename treatment treatment_arm
			tab treatment_arm, mi
			tab treatment_arm, gen(treatment_arm_)
			
			***5 frames + 1 control
			gen treatment_frame = treatment_arm 
			recode treatment_frame (6 = 1) (7 = 2) (8 = 3) (9 = 4) (10 = 5)
			la val treatment_frame treatment_frame
			la var treatment_frame "Treatment message frames"
			tab treatment_frame, gen(treatment_frame_)
			
			***2 frequencies + 1 control
			gen treatment_timing = treatment_arm
			recode treatment_timing (1/5 = 1) (6/10 = 2)
			la val treatment_timing treatment_timing
			la var treatment_timing "Treatment message timing"
			tab treatment_timing, gen(treatment_timing_)
			
			***1 pooled treatment + 1 control
			gen byte treatment_pooled = treatment_arm > 0
			replace treatment_pooled = . if missing(treatment_arm)
			la val treatment_pooled treatment_pooled
			la var treatment_pooled "Treatment"
			
			global treat_pool treatment_pooled
			global treat_frames treatment_frame_2 treatment_frame_3 treatment_frame_4 treatment_frame_5 treatment_frame_6
			global treat_timings treatment_timing_2 treatment_timing_3
			global treat_arms treatment_arm_2 treatment_arm_3 treatment_arm_4 treatment_arm_5 treatment_arm_6 treatment_arm_7 treatment_arm_8 treatment_arm_9 treatment_arm_10 treatment_arm_11
		
			foreach v in $treat_pool $treat_frames $treat_timings $treat_arms {
				gen `v'_sd = `v'*behavior_sd
				gen `v'_hw = `v'*behavior_hw
			}
	
	label data "Merged RI data"
	save "$rianalysis/ri_data_final.dta", replace
